﻿using System.Collections.ObjectModel;
using System.Linq;

namespace SpCop.Core
{
    public class Arguments : Collection<Argument>
    {
        public static Arguments Parse(string[] args)
        {

            var arguments = new Arguments();
            for (int i = 0; i < args.Length; i++)
            {
                string name = args[i];
                if(string.IsNullOrEmpty((name)))
                    continue;

                var arg = new Argument {Name = name};
                int startIndex = name.IndexOfAny(new[] {':', '='});
                if ((name.StartsWith("/") || name.StartsWith("-")) && startIndex > 1 )
                {
                    arg.Name = name.Substring(1, startIndex - 1);
                    arg.Value = name.Substring(startIndex + 1);
                }
                else
                {
                    if(name.Length > 1)
                        arg.Name = name.Substring(1);
                    else
                        arg.Name = name;
                }
                arguments.Add(arg);
            }
            return arguments;
        }


        public bool ContainsArgument(string key)
        {

            return this.Any(a => a.Name.ToUpperInvariant() == key.ToUpperInvariant());

        }

        public string GetArgumentValue(string key)
        {
            return GetArgument(key).Value;
        }

        public Argument GetArgument(string key)
        {
            return this.FirstOrDefault(a => a.Name.ToUpperInvariant() == key.ToUpperInvariant());
        }
    }
}